package com.hdcx.Minfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.bean.CodeWrapper;
import com.hdcx.Minfresh.bean.RegisterUserInfo;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class ResetPswActivity extends BaseActivity {

    EditText ed_psw1, ed_psw2, ed_phone, ed_num;
    String num;//验证码
    Button bt_send;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_regetpsw);
        initView();
        initData();
    }

    private void initView() {
        ed_psw1 = (EditText) findViewById(R.id.ed_psw1);
        ed_psw2 = (EditText) findViewById(R.id.ed_psw2);
        ed_phone = (EditText) findViewById(R.id.ed_phone);
        ed_num = (EditText) findViewById(R.id.ed_num);
        bt_send = (Button) findViewById(R.id.bt_send);
        findViewById(R.id.bt_register).setOnClickListener(this);
        bt_send.setOnClickListener(this);
        findViewById(R.id.tv_left).setOnClickListener(this);
    }

    private void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_send:
                post_get_code();
                break;
            case R.id.bt_register:
                register();
                break;
            case R.id.tv_left:
                finish();
                break;
        }
    }
    /*  mobile	登录手机号码
      password 登录密码   //加密后的传上来  加密方式  md5(密码+‘huudon’)
      repassword 确认密码*/
    private void register() {
        String phone = ed_phone.getText().toString().trim();
        final String password1 = ed_psw1.getText().toString().trim();
        final String password2 = ed_psw2.getText().toString().trim();
        String code = ed_num.getText().toString().trim();

        if(!AppUtil.isPhone(phone)){
            Toast.makeText(this, "请输入正确的手机号码！", Toast.LENGTH_SHORT).show();
            return;
        }if(!password1.equals(password2)){
            Toast.makeText(this, "两次密码不一致！", Toast.LENGTH_SHORT).show();
            return;
        }if(!code.equals(num)){
            Toast.makeText(this, "验证码不正确！", Toast.LENGTH_SHORT).show();
            return;
        }if(password1.equals("")){
            Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        JSONObject json = new JSONObject();
        try {
            json.put("mobile", phone);
            json.put("password",AppUtil.getMD5(password1+"huudon"));
            json.put("repassword",AppUtil.getMD5(password2+"huudon"));
            Log.i("password", "register: "+AppUtil.getMD5(password1+"huudon"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.SETPWD)
                .content(json.toString())
                .build()
                .execute(new Callback<RegisterUserInfo>() {
                    @Override
                    public RegisterUserInfo parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, RegisterUserInfo.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(RegisterUserInfo response) {
                        if (response.getStatus() == 1) {
                            Toast.makeText(ResetPswActivity.this, "密码重置成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        }else{
                            Toast.makeText(ResetPswActivity.this, "服务器繁忙！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    /**
     * 获取验证码
     * mobile	手机号码
     type 验证类型 reg、forget
     */

    public void post_get_code() {
        JSONObject json = new JSONObject();
        if(!AppUtil.isPhone(ed_phone.getText().toString().trim())){
            Toast.makeText(this, "请输入正确的手机号码！", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            json.put("mobile", ed_phone.getText().toString().trim());
            json.put("type","forget");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.GETCODE)
                .content(json.toString())
                .build()
                .execute(new Callback<CodeWrapper>() {
                    @Override
                    public CodeWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, CodeWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(CodeWrapper response) {
                        if (response.getStatus() == 1) {
                            num = response.getCode();
                            //ed_num.setText(response.getCode());
                            bt_send.postDelayed(timeRun, 1000);
                            bt_send.setEnabled(false);
                            Toast.makeText(ResetPswActivity.this, "请求已发送，请耐心等待验证码！", Toast.LENGTH_SHORT).show();
                        } else {
                            String errorMsg = response.getInfo();
                            Toast.makeText(ResetPswActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
    /*  **
              * 按钮倒计时
      *//*
*/
    private int time = 60;
    Runnable timeRun = new Runnable() {

        @Override
        public void run() {
            --time;
            bt_send.setText(time + "秒后再次发送");

            if (time <= 0) {
                bt_send.setEnabled(true);
                bt_send.setText("获取短信验证码");
                time = 60;
                return;
            }
            bt_send.postDelayed(timeRun, 1000);
        }
    };
}
